package com.youhome.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by chen365 on 2019/01/07.
 */
public class StringUtils extends org.springframework.util.StringUtils {
    /**
     * 查询字符串在数组中的位置
     *
     * @param arrays 字符串数组
     * @param key    需要查找的字符串
     * @return -1表示数组中不存在查找的字符串,否则返回字符串所在位置
     */
    public static int getPosition(String[] arrays, String key) {
        int i = 0;
        for (String item : arrays) {
            if (item.equals(key)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public static String getMarital(String val) {
        String marital = "";
        if (val.equals("0")) {
            marital = "保密";
        } else if (val.equals("1")) {
            marital = "已婚";
        } else if (val.equals("2")) {
            marital = "未婚";
        } else if (val.equals("3")) {
            marital = "离异";
        }
        return marital;
    }

    /**
     * 注册来源 转换
     *
     * @param source 1.分享注册,2.顾问创建,3.B端注册,4.C端注册
     * @return
     */
    public static String getSourceText(Integer source) {
        String sourceText = "C端注册";
        switch (source) {
            case 1: {
                sourceText = "分享注册";
                break;
            }
            case 2: {
                sourceText = "顾问创建";
                break;
            }
            case 3: {
                sourceText = "B端注册";
                break;
            }
            case 4: {
                sourceText = "C端注册";
                break;
            }
            default: {
                sourceText = "C端注册";
            }
        }
        return sourceText;
    }




    /**
     * 排序字段转为数据字段
     *
     * @param filed
     * @return
     */
    public static String transitionOrderBy(String filed) {

        //bedrooms：卧室数量;bathrooms：浴数量;yearBuilt：房龄;area：面积;listPrice:价格
        switch (filed) {
            case "yearBuilt":
                filed = "year_built";
                break;
            case "listPrice":
                filed = "list_price";
                break;
            case "livingArea":
                filed = "living_area";
                break;
            case "bathroomCount":
                filed = "bathroom_count";
                break;
            case "bedroomCount":
                filed = "bedroom_count";
                break;
            default:
                filed = "year_built";
                break;
        }
        return filed;
    }

    public static String getSex(String val) {
        String sex = "";
        if (val.equals("0")) {
            sex = "女";
        } else if (val.equals("1")) {
            sex = "男";
        }
        return sex;
    }

    /**
     * 首字母转大小写
     *
     * @param s
     * @return
     */
    public static String toUpperCaseIndex(String s) {
        s = s.toLowerCase();
        char[] cs = s.toCharArray();
        // 先将第一个下标的字符转换成大写
        cs[0] = Character.toUpperCase(cs[0]);
        for (int i = 0; i < cs.length - 1; i++) {
            if (cs[i] == ' ') {
                cs[i + 1] = Character.toUpperCase(cs[i + 1]);
            }
        }
        return String.valueOf(cs);
    }

    /**
     * 生成随机文件名：当前年月日时分秒+五位随机数
     *
     * @return
     */
    public static String getDateRandomName() {
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数

        return str + rannum;// 当前时间
    }


    public static String replaceBlankAndComma2(String name) {
        return name.replaceAll("-|\\. |\\.|, |,| ", "_").toLowerCase();
    }

    public static String replaceBlankAndComma(String name) {

        return name
                .trim()
                .toLowerCase()
                .replaceAll("[^0-9|a-z]", "_")
                .replaceAll("_{2,}", "_");
    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

    public static String addZeroForNum(String str, int strLength, Boolean pos) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                if (pos) {
                    sb.append("0").append(str);// 左补0
                } else {
                    sb.append(str).append("0");//右补0
                }
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }

    public static String getZip(List<String> zips, String zip) {
        Integer a = Integer.valueOf(zip);
        Integer nearest = null; //最接近的Zip
        int i = 0;
        for (String number : zips) {
            if (i == 0) {
                nearest = Integer.valueOf(number);
            } else {
                if ((Math.abs(a - nearest)) >= (Math.abs(a - Integer.valueOf(number)))) {
                    nearest = Integer.valueOf(number);
                }
            }
            i++;
        }
        return addZeroForNum(nearest.toString(), 5);
    }

    /**
     * 判断是否为热门城市
     *
     * @param keyword 城市名称
     * @return
     */
    public static boolean isHotCity(String keyword) {
        if (keyword.equals("San Francisco") ||
                keyword.equals("Los Angeles") ||
                keyword.equals("New York") ||
                keyword.equals("Seattle") ||
                keyword.equals("Atlanta") ||
                keyword.equals("Boston") ||
                keyword.equals("New Jersey") ||
                keyword.equals("Houston") ||
                keyword.equals("San Diago") ||
                keyword.equals("Orlando")
                ) {
            return true;
        }
        return false;
    }


    /**
     * 州全写转简写
     *
     * @param s
     * @return
     */
    public static String stateAbbreviationToAll(String s) {
        String state;
        switch (s.toUpperCase()) {
            case "AL": {
                state = "Alabama";
                break;
            }
            case "AK": {
                state = "Alaska";
                break;
            }
            case "AZ": {
                state = "Arizona";
                break;
            }
            case "AR": {
                state = "Arkansas";
                break;
            }
            case "CA": {
                state = "California";
                break;
            }
            case "CO": {
                state = "Colorado";
                break;
            }
            case "CT": {
                state = "Connecticut";
                break;
            }
            case "DE": {
                state = "Delaware";
                break;
            }
            case "FL": {
                state = "Florida";
                break;
            }
            case "GA": {
                state = "Georgia";
                break;
            }
            case "HI": {
                state = "Hawaii";
                break;
            }
            case "ID": {
                state = "Idaho";
                break;
            }
            case "IL": {
                state = "Illinois";
                break;
            }
            case "IN": {
                state = "Indiana";
                break;
            }
            case "IA": {
                state = "Iowa";
                break;
            }
            case "KS": {
                state = "Kansas";
                break;
            }
            case "KY": {
                state = "Kentucky";
                break;
            }
            case "LA": {
                state = "Louisiana";
                break;
            }
            case "ME": {
                state = "Maine";
                break;
            }
            case "MD": {
                state = "Maryland";
                break;
            }
            case "MA": {
                state = "Massachusetts";
                break;
            }
            case "MI": {
                state = "Michigan";
                break;
            }
            case "MN": {
                state = "Minnesota";
                break;
            }
            case "MS": {
                state = "Mississippi";
                break;
            }
            case "MO": {
                state = "Missouri";
                break;
            }
            case "MT": {
                state = "Montana";
                break;
            }
            case "NE": {
                state = "Nebraska";
                break;
            }
            case "NV": {
                state = "Nevada";
                break;
            }
            case "NH": {
                state = "New Hampshire";
                break;
            }
            case "NJ": {
                state = "New Jersey";
                break;
            }
            case "NM": {
                state = "New Mexico";
                break;
            }
            case "NY": {
                state = "New York";
                break;
            }
            case "NC": {
                state = "North Carolina";
                break;
            }
            case "ND": {
                state = "North Dakota";
                break;
            }
            case "OH": {
                state = "Ohio";
                break;
            }
            case "OK": {
                state = "Oklahoma";
                break;
            }
            case "OR": {
                state = "Oregon";
                break;
            }
            case "PA": {
                state = "Pennsylvania";
                break;
            }
            case "RI": {
                state = "Rhode Island";
                break;
            }
            case "SC": {
                state = "South Carolina";
                break;
            }
            case "SD": {
                state = "South Dakota";
                break;
            }
            case "TN": {
                state = "Tennessee";
                break;
            }
            case "TX": {
                state = "Texas";
                break;
            }
            case "UT": {
                state = "Utah";
                break;
            }
            case "VT": {
                state = "Vermont";
                break;
            }
            case "VA": {
                state = "Virginia";
                break;
            }
            case "WA": {
                state = "Washington";
                break;
            }
            case "WV": {
                state = "West Virginia";
                break;
            }
            case "WI": {
                state = "Wisconsin";
                break;
            }
            case "WY": {
                state = "Wyoming";
                break;
            }
            default: {
                state = null;
            }
        }
        return state;
    }

    /**
     * 州简写转全写
     *
     * @param s
     * @return
     */
    public static String stateAllToAbbreviation(String s) {
        if (StringUtils.isEmpty(s)) {
            return s;
        }
        String state;
        switch (s.toLowerCase()) {
            case "Alabama": {
                state = "AL";
                break;
            }
            case "Alaska": {
                state = "AK";
                break;
            }
            case "Arizona": {
                state = "AZ";
                break;
            }
            case "Arkansas": {
                state = "AR";
                break;
            }
            case "California": {
                state = "CA";
                break;
            }
            case "Colorado": {
                state = "CO";
                break;
            }
            case "Connecticut": {
                state = "CT";
                break;
            }
            case "Delaware": {
                state = "DE";
                break;
            }
            case "Florida": {
                state = "FL";
                break;
            }
            case "Georgia": {
                state = "GA";
                break;
            }
            case "Hawaii": {
                state = "HI";
                break;
            }
            case "Idaho": {
                state = "ID";
                break;
            }
            case "Illinois": {
                state = "IL";
                break;
            }
            case "Indiana": {
                state = "IN";
                break;
            }
            case "Iowa": {
                state = "IA";
                break;
            }
            case "Kansas": {
                state = "KS";
                break;
            }
            case "Kentucky": {
                state = "KY";
                break;
            }
            case "Louisiana": {
                state = "LA";
                break;
            }
            case "Maine": {
                state = "ME";
                break;
            }
            case "Maryland": {
                state = "MD";
                break;
            }
            case "Massachusetts": {
                state = "MA";
                break;
            }
            case "Michigan": {
                state = "MI";
                break;
            }
            case "Minnesota": {
                state = "MN";
                break;
            }
            case "Mississippi": {
                state = "MS";
                break;
            }
            case "Missouri": {
                state = "MO";
                break;
            }
            case "Montana": {
                state = "MT";
                break;
            }
            case "Nebraska": {
                state = "NE";
                break;
            }
            case "Nevada": {
                state = "NV";
                break;
            }
            case "New Hampshire": {
                state = "NH";
                break;
            }
            case "New Jersey": {
                state = "NJ";
                break;
            }
            case "New Mexico": {
                state = "NM";
                break;
            }
            case "New York": {
                state = "NY";
                break;
            }
            case "North Carolina": {
                state = "NC";
                break;
            }
            case "North Dakota": {
                state = "ND";
                break;
            }
            case "Ohio": {
                state = "OH";
                break;
            }
            case "Oklahoma": {
                state = "OK";
                break;
            }
            case "Oregon": {
                state = "OR";
                break;
            }
            case "Pennsylvania": {
                state = "PA";
                break;
            }
            case "Rhode Island": {
                state = "RI";
                break;
            }
            case "South Carolina": {
                state = "SC";
                break;
            }
            case "South Dakota": {
                state = "SD";
                break;
            }
            case "Tennessee": {
                state = "TN";
                break;
            }
            case "Texas": {
                state = "TX";
                break;
            }
            case "Utah": {
                state = "UT";
                break;
            }
            case "Vermont": {
                state = "VT";
                break;
            }
            case "Virginia": {
                state = "VA";
                break;
            }
            case "Washington": {
                state = "WA";
                break;
            }
            case "West Virginia": {
                state = "WV";
                break;
            }
            case "Wisconsin": {
                state = "WI";
                break;
            }
            case "Wyoming": {
                state = "WY";
                break;
            }
            default: {
                state = s;
            }
        }
        return state;
    }
}
